package com.g7.figmaapi.user.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g7.figmaapi.common.constant.CommonConstant;
import com.g7.figmaapi.common.exception.business.ValidationException;
import com.g7.figmaapi.user.domain.bo.UserAuthorizationBo;
import com.g7.figmaapi.user.domain.entity.UserAuthorization;
import com.g7.figmaapi.user.mapper.UserAuthorizationMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class UserAuthorizationDao extends ServiceImpl<UserAuthorizationMapper, UserAuthorization> {

    /**
     * 根据条件获取用户授权信息（单条，id倒序）
     *
     * @param userAuthorizationBo 用户授权实体
     * @return com.g7.figmaapi.user.domain.entity.UserAuthorization
     * @author taoxiangqian
     * @since 2023/11/8
     **/
    public UserAuthorization getUserAuthorizationOneByParam(UserAuthorizationBo userAuthorizationBo) {
        if (userAuthorizationBo == null) {
            throw new ValidationException();
        }
        return getOne(getLambdaQueryWrapper(userAuthorizationBo), false);
    }

    /**
     * 根据条件查询数据行数
     *
     * @param userAuthorizationBo 用户授权实体
     * @return java.lang.Long
     * @author taoxiangqian
     * @since 2023/11/15
     **/
    public Long getUserAuthorizationCountByParam(UserAuthorizationBo userAuthorizationBo) {
        if (userAuthorizationBo == null) {
            throw new ValidationException();
        }
        return count(getLambdaQueryWrapper(userAuthorizationBo));
    }

    private LambdaQueryWrapper<UserAuthorization> getLambdaQueryWrapper(UserAuthorizationBo userAuthorizationBo) {
        LambdaQueryWrapper<UserAuthorization> wrapper = new QueryWrapper<UserAuthorization>().lambda().orderByDesc(UserAuthorization::getId);

        if (userAuthorizationBo.getUserId() != null) {
            wrapper.eq(UserAuthorization::getUserId, userAuthorizationBo.getUserId());
        }
        if (userAuthorizationBo.getExcludeUserId() != null) {
            wrapper.ne(UserAuthorization::getUserId, userAuthorizationBo.getExcludeUserId());
        }
        if (StringUtils.isNotEmpty(userAuthorizationBo.getChannelType())) {
            wrapper.eq(UserAuthorization::getChannelType, userAuthorizationBo.getChannelType());
        }
        if (StringUtils.isNotEmpty(userAuthorizationBo.getAuthorizationSource())) {
            wrapper.eq(UserAuthorization::getAuthorizationSource, userAuthorizationBo.getAuthorizationSource());
        }

        if (userAuthorizationBo.getIsEnabled() != null) {
            wrapper.eq(UserAuthorization::getIsEnabled, userAuthorizationBo.getIsEnabled());
        } else {
            wrapper.eq(UserAuthorization::getIsEnabled, CommonConstant.DEFAULT_BYTE_YES);
        }
        return wrapper;
    }

    /**
     * 新增或修改用户授权信息
     *
     * @param userAuthorization 用户授权实体
     * @return com.g7.figmaapi.user.domain.entity.UserAuthorization
     * @author taoxiangqian
     * @since 2023/11/05
     */
    public boolean saveOrUpdateUserAuthorization(UserAuthorization userAuthorization) {
        return saveOrUpdate(userAuthorization);
    }
}
